# This file is part of the replication packet for "A Low-Cost Information Nudge 
# Increases Citizenship Application Rates Among Low-Income Immigrants"
#
# this script creates the power curve (figure S5) requested from the editors
#
# Input: None
# Output: power.pdf


# loading in the packages -------------------------------------------------

rm(list=ls())
library(ggplot2)
library(Hmisc)


# setting the location for the figure -------------------------------------

# name of directory
figure_location <- "~/Dropbox (IPL)/NNY_November2017_feewaiver_survey/replication/replication_tables"

# set the directory of the data
setwd(figure_location)


# estimating power --------------------------------------------------------

# creating empty variable to hold the power estimates
powerstore <- c()

# setting mean0 to the application rate in the control group
mean0      <- .25

# creating a range of possible effect sizes
deltas <- seq(-.12,.12,.001)

# loop calculates the power for each effect size
# fixes the number in the study and the mean for the control group
for(i in 1:length(deltas)){
  
  mean1 <- mean0 + deltas[i]
  pp <-bpower(p1=mean0, p2=mean1, 
              n1=370  , n2=1167, alpha=0.05)
  powerstore <- c(powerstore,pp)

}

# combines the effect size with the power
d <- data.frame(deltas,powerstore)


# plotting the power data -------------------------------------------------

p <- ggplot(data=d,aes(x=deltas*100,y=powerstore*100)) +
  geom_line(col="red") + xlab("change in naturalization rate (%)")+
  ylab("statistical power (%)") + ylim(c(0,100)) + theme(
               axis.title=element_text(size=12,face="bold"),
               axis.text=element_text(size=12,face="bold"),
               title=element_text(size=14,face="bold"))
p


# saving the figure -------------------------------------------------------

ggsave("power.pdf",width=8,height=6)
